package spring.boot.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import spring.boot.redis.handler.TestHandler;

@Configuration
public class RedisTopicConfig {

    @Autowired
    private TestHandler testHandler;

    @Bean
    public RedisMessageListenerContainer container(@Qualifier("masterLettuceConnectionFactory") RedisConnectionFactory masterLettuceConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(masterLettuceConnectionFactory);
        //根据channel调用,多个channel这里可以add多个
        container.addMessageListener(testHandler, new PatternTopic(TestHandler.CHANNEL));
        return container;
    }

}
